package com.example.dzcfinal;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.dzcfinal.SQLite.MySQLite;

public class LoginActivity extends Activity implements TextWatcher{
    private EditText username;
    private EditText password;
    private Button loginBtn;
    private String usernameText;
    private String passwordText;
    private Button jumpRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initHandle();
        checkLogin();
        setJumpRegister();
    }

    protected void initHandle(){
        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        loginBtn = findViewById(R.id.loginBtn);
        jumpRegister = findViewById(R.id.jumpRegister);

        username.addTextChangedListener(this);
        password.addTextChangedListener(this);
    }


    protected void checkLogin(){
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                usernameText = username.getText().toString().trim();
                passwordText = password.getText().toString().trim();
                Log.e("TAG", "afterTextChanged: ");
                if (usernameText.equals("")){
                    Toast.makeText(LoginActivity.this,"请输入账号",Toast.LENGTH_SHORT).show();
                }else if (passwordText.equals("")){
                    Toast.makeText(LoginActivity.this,"请输入密码",Toast.LENGTH_SHORT).show();
                }else {
                    MySQLite sql = new MySQLite(LoginActivity.this);
                    if (sql.checkUser(usernameText,passwordText)==-1){
                        Toast.makeText(LoginActivity.this, "用户不存在", Toast.LENGTH_SHORT).show();
                    }else if (sql.checkUser(usernameText,passwordText)==-2){
                        Toast.makeText(LoginActivity.this, "密码错误", Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                        Bundle bundle  =new Bundle();
                        bundle.putInt("userId",sql.checkUser(usernameText,passwordText));
                        bundle.putInt("index",0);
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }
                }
            }
        });
    }

    protected void setJumpRegister(){
        jumpRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        usernameText = username.getText().toString().trim();
        passwordText = password.getText().toString().trim();
        Log.e("TAG", "afterTextChanged: " +passwordText);
        if (!(usernameText.equals(""))&&!(passwordText.equals(""))){
            loginBtn.setBackgroundColor(Color.parseColor("#FF58B3DB"));
            loginBtn.setTextColor(Color.parseColor("#ffffff"));
        }else {
            loginBtn.setBackgroundColor(Color.parseColor("#d6d7d7"));
            loginBtn.setTextColor(Color.parseColor("#000000"));
        }
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
}